package com.zyao89.view.zloading.ball;


import com.zyao89.view.zloading.util.NumCalcUtil;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.Component;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.Path;
import ohos.agp.utils.Color;
import ohos.app.Context;

/**
 * Created by zyao89 on 2017/3/29.
 * Contact me at 305161066@qq.com or zyao89@gmail.com
 * For more projects: https://github.com/zyao89
 * My Blog: http://zyao89.me
 */
public class InfectionBallBuilder extends BaseBallBuilder {
    // 动画间隔时间
    private long mDurationTime = 888;
    private long mDurationTime_1 = 222;
    private long mDurationTime_2 = 333;
    private long mDurationTime_3 = 1333;
    private long mDurationTime_4 = 1333;
    // 最终阶段
    private static final int FINAL_STATE = 4;
    private static final int SUM_POINT_POS = 3;
    private float mBallR;
    private Path mPath;
    // 当前动画阶段
    private int mCurrAnimatorState = 0;
    // 每个小球的偏移量
    private float mCanvasTranslateOffset;


    @Override
    protected void initParams(Context context, Color color) {
        mBallR = getAllSize() / SUM_POINT_POS;
        mCanvasTranslateOffset = getIntrinsicWidth() / SUM_POINT_POS;
        mPath = new Path();

        initPaint(5, color);
        initPoints(mBallR);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        drawBall(canvas);
    }

    /**
     * 绘制小球
     *
     * @param canvas
     */
    private void drawBall(Canvas canvas) {
        canvas.save();
        mPaint.setStyle(Paint.Style.FILLANDSTROKE_STYLE);
        canvas.translate(0, -mCanvasTranslateOffset);
        super.drawBall(canvas, mPath, mPaint);
        canvas.restore();
    }

    @Override
    protected void prepareStart(AnimatorValue floatValueAnimator) {
        mDurationTime = ceil(getAnimationDuration() * 0.7);
        mDurationTime_1 = ceil(getAnimationDuration() * 0.2);
        mDurationTime_2 = ceil(getAnimationDuration() * 0.3);
        mDurationTime_3 = getAnimationDuration();
        mDurationTime_4 = getAnimationDuration();
    }

    @Override
    protected void prepareEnd() {

    }

    @Override
    protected void computeUpdateValue(AnimatorValue animation, float animatedValue) {
        float offset = mCanvasTranslateOffset;
        switch (mCurrAnimatorState) {
            case 0:
                animation.setDuration(mDurationTime);
                mBallPoints.get(2).setOffsetY(animatedValue * offset);
                mBallPoints.get(3).setOffsetY(animatedValue * offset);
                mBallPoints.get(4).setOffsetY(animatedValue * offset);
                break;
            case 1:
                animation.setDuration(mDurationTime_1);
                mBallPoints.get(5).setOffsetY(animatedValue * offset);
                mBallPoints.get(6).setOffsetY(animatedValue * offset);
                mBallPoints.get(7).setOffsetY(animatedValue * offset);
                mBallPoints.get(1).setOffsetY(animatedValue * offset);
                mBallPoints.get(0).setOffsetY(animatedValue * offset);
                mBallPoints.get(11).setOffsetY(animatedValue * offset);
                break;
            case 2:
                animation.setDuration(mDurationTime_2);
                for (int i = 0; i < mBallPoints.size(); i++) {
                    if (i > 10 || i < 8) {
                        mBallPoints.get(i).setOffsetY(NumCalcUtil.add(animatedValue * offset, offset));
                    } else {
                        mBallPoints.get(i).setOffsetY(animatedValue * offset);
                    }
                }
                break;
            case 3:
                animation.setDuration(mDurationTime_3);
                // 无对应的方法
                // animation.setInterpolator(new DecelerateInterpolator());

                mBallPoints.get(8).setOffsetY(NumCalcUtil.add(animatedValue * offset, offset));
                mBallPoints.get(9).setOffsetY(NumCalcUtil.add(animatedValue * offset, offset));
                mBallPoints.get(10).setOffsetY(NumCalcUtil.add(animatedValue * offset, offset));

                mBallPoints.get(5).setOffsetX(animatedValue * offset);
                mBallPoints.get(6).setOffsetX(animatedValue * offset);
                mBallPoints.get(7).setOffsetX(animatedValue * offset);
                mBallPoints.get(1).setOffsetX(-animatedValue * offset);
                mBallPoints.get(0).setOffsetX(-animatedValue * offset);
                mBallPoints.get(11).setOffsetX(-animatedValue * offset);
                break;
            case 4:
                animation.setDuration(mDurationTime_4);
                mPaint.setAlpha(NumCalcUtil.subtract(1f, animatedValue) * 1f);
                break;
            default:
                break;
        }
    }

    @Override
    public void onRepeat(Animator animator) {
        if (++mCurrAnimatorState > FINAL_STATE) { // 还原到第一阶段
            mCurrAnimatorState = 0;
            for (CirclePoint point : mBallPoints) {
                point.setOffsetY(0);
                point.setOffsetX(0);
            }
            mPaint.setAlpha(1f);
        }
    }

}
